package com.project.yuepai.controller.admin;

import com.project.yuepai.dto.MessageBean;
import com.project.yuepai.model.Admin;
import com.project.yuepai.service.AdminService;
import com.project.yuepai.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/login.action",method = RequestMethod.POST)
    public MessageBean login(String adminName, String adminPwd, HttpSession session){
        System.out.println(adminName+" "+adminPwd);
        Admin admin = adminService.checkAdmin(adminName,adminPwd);
        if(admin==null)
            return MessageUtil.failure("管理员名称或密码错误！");
        session.setAttribute("admin",admin);
        return MessageUtil.success("登录成功！");
    }

    //退出登录
    @RequestMapping(value = "/logout.action",method = RequestMethod.GET)
    public MessageBean logout(HttpSession session){
        session.invalidate();
        return MessageUtil.success("退出成功！");
    }


}
